package com.auto.admin.controller.api;

import com.auto.admin.entity.bo.ApiInfoBO;
import com.auto.admin.entity.bo.param.BeansBO;
import com.auto.admin.manager.ApiInfoManager;
import com.auto.admin.manager.BeansManager;
import com.auto.admin.manager.TornaDocPushManager;
import com.auto.api.admin.api.ApiInfoApi;
import com.auto.api.admin.dto.AdminApiDTO;
import com.auto.api.admin.dto.result.ApiInfoDTO;
import com.auto.api.common.entity.NullObj;
import com.auto.api.common.entity.ResultData;
import com.auto.api.common.utils.BeanMapper;
import java.util.List;
import javax.annotation.Resource;

import com.auto.api.common.utils.JsonMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * Description:ApiInfoApiController接口
 *
 * @author liangshf
 * @date 2018-9-26
 */
@Slf4j
@RestController
public class ApiInfoApiController implements ApiInfoApi {

    @Resource
    private ApiInfoManager apiInfoManager;

    @Resource
    private TornaDocPushManager tornaDocPushManager;

    @Resource
    private BeansManager beansManager;

    @Override
    public ResultData<NullObj> saveApiList(@RequestBody final AdminApiDTO adminApi) {
        log.error("saveApiList 入参：{}", JsonMapper.toJson(adminApi));
        if (CollectionUtils.isEmpty(adminApi.getAdminApiList())) {
            return ResultData.ok();
        }
        List<ApiInfoBO> mappingList = BeanMapper.mapList(adminApi.getAdminApiList(), ApiInfoBO.class);
        apiInfoManager.saveApiList(mappingList);
        beansManager.saveBeans(BeanMapper.mapList(adminApi.getBeansList(), BeansBO.class));
        tornaDocPushManager.pushTornaDoc(mappingList);
        return ResultData.ok();
    }

    @Override
    public ResultData<ApiInfoDTO> getApiByUrl(String url) {
        return ResultData.ok(BeanMapper.map(apiInfoManager.getByUrl(url), ApiInfoDTO.class));
    }

}